package util;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;

public class ViewUtil {

  private static int alpha = 255;
  private static int increment = -5;
  private static Timer timeFadeInOutLabel;
  //
  public static JTextField text;
  public static JComboBox combox;
  public static JFormattedTextField formatted;

  public static void trocaPanels(JPanel p1, JPanel p2) {
    p1.removeAll();
    p2.setSize(new Dimension(p1.getWidth(), p1.getHeight()));
    p1.add(p2);
    p1.validate();
    p1.repaint();
    p2.setVisible(true);
  }

  public static boolean isEmpty(Component[] comp) {

    boolean resultado = true;

    for (Component comp1 : comp) {
      if (comp1 instanceof JTextField) {
        text = (JTextField) comp1;
        if (text.getText().isEmpty()) {
          text.grabFocus();
          resultado = false;
        }
      }
      if (comp1 instanceof JComboBox) {
        combox = (JComboBox) comp1;
        if (combox.getSelectedIndex() == -1) {
          combox.grabFocus();
          resultado = false;
        }
      }
      if (comp1 instanceof JFormattedTextField) {
        formatted = (JFormattedTextField) comp1;
        if (UrnaUtil.retirarSimbolos(formatted.getText()).isEmpty()) {
          formatted.grabFocus();
          resultado = false;
        }
      }
    }
    return resultado;
  }

  /*    
   Gera um efeito Fade IN e Fade Out na borda da Label;
   */
  public static void startFadeInOutBorderLabel(JLabel label) {

    timeFadeInOutLabel = new Timer(15, new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        alpha += increment;

        if (alpha >= 255) {
          alpha = 255;
          increment = -increment;
        }
        if (alpha <= 0) {
          alpha = 0;
          increment = -increment;
        }
        label.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0, alpha), 1));
      }
    });
    //
    timeFadeInOutLabel.start();
  }

  /*    
   Para o efeito Fade IN e Fade Out na borda da Label;
   */
  public static void stopFadeInOutBorderLabel(JLabel label) {
    if (timeFadeInOutLabel != null) {  
      timeFadeInOutLabel.stop();
    }  
    label.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0, 255), 1));
  }

  /*
   Carrega a foto passada no endereço no Label Passado
   */
  public static void loadFoto(JLabel label, String endereco) {
       
    BufferedImage bi;
    BufferedImage aux;
    Graphics2D g;
    AffineTransform at;
    File f;
    ImageIcon i;
    //
    try {
      f   = new File(UrnaUtil.formatFotoEnd(endereco));
      bi  = ImageIO.read(f);
      aux = new BufferedImage(label.getSize().width, label.getSize().height, bi.getType());
      g   = aux.createGraphics();//pega a classe graphics do aux para edicao  
      at  = AffineTransform.getScaleInstance((double) label.getSize().width / bi.getWidth(), (double) label.getSize().height / bi.getHeight());//cria a transformacao  
      g.drawRenderedImage(bi, at);//pinta e transforma a imagem real no auxiliar  
      i = new ImageIcon(aux);
      label.setIcon(i);// seta no jlabel  
    } catch (IOException ex) {
      Logger.getLogger(ViewUtil.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
}
